import express from 'express'
import { getAccount, hasWebLogin } from '../../../middleWare/user/index.js';
import { addSql, countSql, getResult, listSql, putSql } from '../../../util/operateSql.js'

var router = express.Router();
const table = 'activity_master.t_pendding_task'

router.get('/list', getAccount, (req, res, next) => {
  const { id:users_id } = req.master
  if (req.master) {
    const sql = listSql(table,{pageSize: 20,users_id,orderBy: 'update_time'})
    getResult(sql).then(async (result) => {
      
      const sql = countSql(table,{...req.query,users_id,orderBy:'update_time'})
      const count = await getResult(sql)
      res.json(req.app.locals.success({list:result,total:count[0].count}))
    });
  } else {
    res.json(req.app.locals.error('用户不存在'))
  }
})
router.post('/add', hasWebLogin, (req, res, next) => {
  const { title,content } = req.body

  if (req.account) {
    const sql = addSql(table,{title,users_id: req.account.id,content})
    getResult(sql).then((result) => {
      
      res.json(req.app.locals.success('新增成功'))
    });
  } else {
    res.json(req.app.locals.error('用户不存在'))
  }
})
router.put('/update', hasWebLogin, (req, res, next) => {
  const { title,content,id,status } = req.body

  if (req.account) {
    const sql = putSql(table,{status},{id})
    getResult(sql).then((result) => {
      
      res.json(req.app.locals.success('修改成功'))
    });
  } else {
    res.json(req.app.locals.error('用户不存在'))
  }
})

export default router